package helpers

import (
  "io"
  "mime/multipart"
  "os"
)

type Uploader struct {
}

func NewUploader() *Uploader {
  return &Uploader{}
}

func (loader *Uploader) UploadFile(file *multipart.FileHeader) (string, error) {
  upload_dir, _ := os.LookupEnv("upload_tmp_dir")

  f, err := file.Open()
  if err != nil {
    return "", err
  }
  defer f.Close()

  filename := file.Filename
  t, err := os.Create(upload_dir + "/" + filename)
  if err != nil {
    return "", err
  }

  defer t.Close()

  if _, err := io.Copy(t, f); err != nil {
    return "", err
  }

  return upload_dir + "/" + filename, nil
}
